"""项目成员管理相关Schema定义"""
from datetime import datetime
from typing import Dict, List, Optional, Union

from pydantic import BaseModel, Field

from app.models.project import ProjectMemberRole


class TeamMemberProjectInfo(BaseModel):
    """团队成员所属项目简要信息"""

    id: str = Field(..., description="项目ID")
    name: str = Field(..., description="项目名称")
    status: Optional[str] = Field(None, description="项目状态代码")
    priority: Optional[str] = Field(None, description="项目优先级代码")
    is_archived: Optional[bool] = Field(None, description="项目是否已归档")


class TeamMemberUserInfo(BaseModel):
    """团队成员用户信息"""

    id: str = Field(..., description="用户ID")
    username: Optional[str] = Field(None, description="用户名")
    name: Optional[str] = Field(None, description="姓名")
    email: Optional[str] = Field(None, description="邮箱")
    avatar: Optional[str] = Field(None, description="头像URL")
    status: Optional[str] = Field(None, description="用户状态")
    department: Optional[str] = Field(None, description="部门")
    position: Optional[str] = Field(None, description="职位")


class TeamMemberRecord(BaseModel):
    """团队成员列表响应记录"""

    project: TeamMemberProjectInfo
    member: TeamMemberUserInfo
    roles: List[str] = Field(default_factory=list, description="项目内角色ID列表")
    role_names: List[str] = Field(default_factory=list, description="项目内角色名称列表")
    role_codes: List[str] = Field(default_factory=list, description="项目内角色代码列表")
    permissions: List[str] = Field(default_factory=list, description="项目内权限列表")
    joined_at: datetime = Field(..., description="加入时间")


class TeamMemberCreateRequest(BaseModel):
    """创建团队成员请求"""

    project_id: str = Field(..., description="项目ID")
    user_id: str = Field(..., description="用户ID")
    roles: Optional[List[str]] = Field(None, description="项目内角色ID列表")
    role: Optional[str] = Field(None, description="项目角色代码（单个角色，用于向后兼容）")
    permissions: List[str] = Field(default_factory=list, description="项目权限列表")


class TeamMemberUpdateRequest(BaseModel):
    """更新团队成员信息请求"""

    roles: Optional[List[str]] = Field(None, description="项目内角色ID列表")
    role: Optional[str] = Field(None, description="项目角色代码（单个角色，用于向后兼容）")
    permissions: Optional[List[str]] = Field(
        None, description="项目权限列表（覆盖更新）"
    )


class TeamMemberStatistics(BaseModel):
    """团队成员统计信息"""

    total_members: int = Field(0, description="成员总数")
    active_members: int = Field(0, description="活跃成员数")
    inactive_members: int = Field(0, description="禁用成员数")
    by_role: Dict[str, int] = Field(default_factory=dict, description="按角色统计")
    by_project: List[Dict[str, Union[str, int]]] = Field(
        default_factory=list, description="按项目统计"
    )


class TeamMemberListResponse(BaseModel):
    """团队成员列表响应"""

    items: List[TeamMemberRecord] = Field(default_factory=list)
    total: int = Field(0)
    page: int = Field(1)
    size: int = Field(20)
    statistics: TeamMemberStatistics = Field(default_factory=TeamMemberStatistics)


